package com.yanqu.road.server.manger.kowloon.battle.role.bullet;

import com.yanqu.road.entity.kowloon.common.Vec2;
import com.yanqu.road.server.manger.kowloon.battle.KowloonBattleFunc;
import com.yanqu.road.server.manger.kowloon.battle.KowloonGame;
import com.yanqu.road.server.manger.kowloon.battle.role.enemy.KowloonEnemy;

public class KowloonBulletSpear extends KowloonBullet{

    private double degree;

    private double endTime;

    private Vec2 endPos = new Vec2();

    public KowloonBulletSpear(KowloonGame game) {
        super(game);
    }

    @Override
    public void init() {
        this.degree = 0;
        this.endTime = game.getGameTime() + this.shooter.cdTime;
    }

    @Override
    public void updateAI(double dt) {
//        this.degree += dt * Math.floor(360000 / this.shooter.cdTime);
        this.degree = 360 * (game.getGameTime() + this.shooter.cdTime - this.endTime) / this.shooter.cdTime;
        this.angle = Math.PI * this.degree / 180;
        this.endPos.setX(this.pos.getX() + this.shooter.data.getProperty().getWeaponRadius() * Math.cos(this.angle));
        this.endPos.setY(this.pos.getY() + this.shooter.data.getProperty().getWeaponRadius() * Math.sin(this.angle));
        KowloonEnemy enemy = this.getHitEnemy();
        if (enemy != null) {
            this.enemyIds.add(enemy.id);
            enemy.beHurt(this.bulletType, +this.shooter.data.getProperty().getAttack());
            enemy.addBuffByBullet(this);
        }
        if (game.gameTime >= this.endTime) {
            this.destroy();
        }
    }

    @Override
    public KowloonEnemy getHitEnemy() {
        for (KowloonEnemy enemy : game.getEnemys()) {
            if (enemy.isAlive()) {
                //直线是否相交
                if (KowloonBattleFunc.lineRect(this.pos, this.endPos, enemy.getHurtRect())) {
                    if (!this.enemyIds.contains(enemy.id)) {
                        return enemy;
                    }
                }
            }
        }
        return null;
    }


}
